﻿package net.wangit.view.tree;

import java.util.HashMap;
import java.util.Map;

import net.wangit.MapList;
import net.wangit.Row;
import net.wangit.jdbc.DB;
import net.wangit.jdbc.JDBCException;
import net.wangit.util.Checker;
import net.wangit.util.StringUtil;
import net.wangit.view.tree.config.TreeActionType;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class TreeUtil {

	private static final Log log = LogFactory
			.getLog(net.wangit.view.tree.TreeUtil.class);

	public TreeUtil() {
	}

	public static Tree getTree(MapList data) {
		if (Checker.isEmpty(data))
			return null;
		int rootRowIndex = data.findRowIndex("pid", "");
		if (rootRowIndex < 0)
			rootRowIndex = data.findRowIndex("pid", null);
		if (rootRowIndex < 0) {
			log.error("could not found the root of tree");
			return null;
		} else {
			return getTree(data, rootRowIndex);
		}
	}

	public static Tree getTree(MapList data, int index) {
		Tree tree = new Tree();
		Row row = data.getRow(index);
		String id = row.get("id");
		tree.setId(id);
		tree.setName(row.get("n"));
		tree.setAction(row.get("a"));
		tree.setActionType(row.getInt("am", TreeActionType.LINK));
		tree.setTarget(row.get("at"));
		tree.setCss(row.get("css"));
		tree.setTitle(row.get("t"));
		tree.setIcon(row.get("ic"));
		tree.setOpenIcon(row.get("io"));
		tree.setCheckValue(row.get("cv"));
		tree.setCheckAttribute(row.get("cc"));
		tree.setAttribute(row.get("c"));
		String filterString = row.get("fu");
		String filterArray[] = StringUtil.split(filterString, "/");
		if (filterArray != null && filterArray.length >= 2) {
			tree.setTargetDomain(filterArray[0]);
			tree.setTargetUnitId(filterArray[1]);
			if (filterArray.length > 2 && !Checker.isEmpty(filterArray[2]))
				tree.setTargetUnitShowMode(filterArray[2]);
			else
				tree.setTargetUnitShowMode("s");
		}
		tree.setFilterMode(row.getInt("fr", 0));
		for (int i = 0; i < data.size(); i++) {
			String parent = data.getRow(i).get("pid");
			if (id.equals(parent))
				tree.add(getTree(data, i));
		}

		return tree;
	}

	public static MapList filterParentId(DB db, String sql, String id,
			String parentIdColumnName, int type) throws JDBCException {
		MapList list = db.query(sql, id, type);
		Map disabled = new HashMap();
		disabled.put(id, null);
		for (int i = 0; i < list.size(); i++) {
			Row row = list.getRow(i);
			String parentId = row.get(parentIdColumnName);
			if (disabled.containsKey(parentId)) {
				disabled.put(row.get("value"), null);
				list.removeRow(i);
				i = -1;
			}
		}

		return list;
	}

	public static MapList getEnumeration(MapList list, String indentString,
			String idKey, String nameKey, String parentKey) {
		if (list == null)
			return null;
		MapList enumeration = new MapList();
		int rootRowIndex = list.findRowIndex(parentKey, "");
		if (rootRowIndex < 0)
			rootRowIndex = list.findRowIndex(parentKey, null);
		setEnumeration(enumeration, list, rootRowIndex, 0, indentString, idKey,
				nameKey, parentKey);
		return enumeration;
	}

	private static void setEnumeration(MapList enumeration, MapList list,
			int index, int indent, String indentString, String idKey,
			String nameKey, String parentKey) {
		Row row = list.getRow(index);
		String id = row.get(idKey);
		StringBuffer treeName = new StringBuffer();
		for (int i = 0; i < indent; i++)
			treeName.append(indentString);

		treeName.append(row.get(nameKey));
		int size = enumeration.size();
		enumeration.put(size, "value", id);
		enumeration.put(size, "name", treeName.toString());
		int nextIndent = indent + 1;
		for (int i = 0; i < list.size(); i++) {
			String parentId = list.getRow(i).get(parentKey);
			if (id.equals(parentId))
				setEnumeration(enumeration, list, i, nextIndent, indentString,
						idKey, nameKey, parentKey);
		}

	}

}
